package main

import (
	"gim/storage"
	"gim/comm"
	"encoding/gob"
	"time"
	"flag"
	log "github.com/golang/glog"
)

var (
	storage_pool *storage.StorageConnPool
)
func main() {
	flag.Parse()
	f := storage.DialStorageFun("127.0.0.1:8088")
	storage_pool = storage.NewStorageConnPool(100, 500, 600 * time.Second, f)
	
	storage, _ := storage_pool.Get()
	defer storage_pool.Release(storage)
	
	gob.Register(comm.IMMessage{})
	
	m := new(comm.Message)
	m.Cmd = comm.MSG_IM
	m.Seq = comm.CurrentTimeMillis()
	m.Version = comm.DEFAULT_VERSION
	m.Body = comm.IMMessage{
		Sender : 1,
		Receiver : 2,
		Content : "hello 2!",
		Extra : "nice job",
	}
	
//	var msgid int64
//	err := storage.Conn.Call("StorageServer.SaveMessage", m, &msgid)
//	if err != nil {
//		log.Error("storage save err", err)
//	}
//	log.Infof("%d", msgid)
	
//	ack_m := new(comm.MessagePeerACK)
//	ack_m.Sender = 1
//	ack_m.Receiver = 2
//	ack_m.Msgid = msgid - 1
	
//	var b bool
//	err = storage.Conn.Call("StorageServer.AckMessage", ack_m, &b)
//	if err != nil {
//		log.Error("storage ack err", err)
//	}
	
	appUid := new(comm.AppUserID)
	appUid.Uid = 2
	
	msg := make([]comm.Message, 0, 5)
	
	err := storage.Conn.Call("StorageServer.GetMessages", appUid, &msg)
	if err != nil {
		log.Error("storage get err", err)
	}
	
	log.Infof("%v", msg)
}